{% extends 'admin/base.html' %}

{% block title %}用户管理 - 管理后台{% endblock %}
{% block page_title %}用户管理{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2><i class="bi bi-people"></i> 用户管理</h2>
            <div>
                <a href="{% url 'bike:user_add' %}" class="btn btn-primary me-2">
                    <i class="bi bi-person-plus"></i> 添加用户
                </a>
                <a href="{% url 'bike:export_users_excel' %}" class="btn btn-outline-success me-2">
                    <i class="bi bi-file-earmark-excel"></i> 导出Excel
                </a>
                <a href="{% url 'bike:admin_dashboard' %}" class="btn btn-outline-secondary">
                    <i class="bi bi-arrow-left"></i> 返回仪表盘
                </a>
            </div>
        </div>

        <!-- 用户列表 -->
        <div class="card">
            <div class="card-body">
                {% if page_obj %}
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>用户名</th>
                                <th>所属学院</th>
                                <th>学院管理员</th>
                                <th>超级管理员</th>
                                <th>创建时间</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for user in page_obj %}
                            <tr>
                                <td>
                                    <span class="fw-bold">{{ user.user.username }}</span>
                                </td>
                                <td>
                                    <span class="badge bg-info">{{ user.get_college_display }}</span>
                                </td>
                                <td>
                                    {% if user.is_admin %}
                                    <span class="badge bg-success">是</span>
                                    {% else %}
                                    <span class="badge bg-secondary">否</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if user.is_super_admin %}
                                    <span class="badge bg-danger">是</span>
                                    {% else %}
                                    <span class="badge bg-secondary">否</span>
                                    {% endif %}
                                </td>
                                <td>
                                    <small class="text-muted">{{ user.created_at|date:"Y-m-d H:i" }}</small>
                                </td>
                                <td>
                                    <div class="btn-group" role="group">
                                        <a href="{% url 'bike:user_edit' user.id %}" class="btn btn-sm btn-outline-primary" title="编辑用户">
                                            <i class="bi bi-pencil"></i> 编辑
                                        </a>
                                        {% if user.id != college_user.id %}
                                        <a href="{% url 'bike:user_delete' user.id %}" class="btn btn-sm btn-outline-danger" title="删除用户" onclick="return confirm('确定要删除用户 {{ user.user.username }} 吗？此操作不可撤销！')">
                                            <i class="bi bi-trash"></i> 删除
                                        </a>
                                        {% endif %}
                                    </div>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>

                <!-- 分页 -->
                {% if page_obj.has_other_pages %}
                <nav aria-label="分页导航" class="mt-4">
                    <ul class="pagination justify-content-center">
                        <!-- 首页 -->
                        {% if page_obj.has_previous %}
                        <li class="page-item">
                            <a class="page-link" href="?page=1">
                                <i class="bi bi-chevron-double-left"></i> 首页
                            </a>
                        </li>
                        {% endif %}

                        <!-- 上一页 -->
                        {% if page_obj.has_previous %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.previous_page_number }}">
                                <i class="bi bi-chevron-left"></i> 上一页
                            </a>
                        </li>
                        {% endif %}

                        <!-- 页码显示 -->
                        {% for num in page_obj.paginator.page_range %}
                            {% if page_obj.number == num %}
                            <li class="page-item active">
                                <span class="page-link">{{ num }}</span>
                            </li>
                            {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ num }}">{{ num }}</a>
                            </li>
                            {% endif %}
                        {% endfor %}

                        <!-- 下一页 -->
                        {% if page_obj.has_next %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.next_page_number }}">
                                下一页 <i class="bi bi-chevron-right"></i>
                            </a>
                        </li>
                        {% endif %}

                        <!-- 末页 -->
                        {% if page_obj.has_next %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">
                                末页 <i class="bi bi-chevron-double-right"></i>
                            </a>
                        </li>
                        {% endif %}
                    </ul>
                    
                    <!-- 分页信息 -->
                    <div class="text-center text-muted small mt-2">
                        显示第 {{ page_obj.start_index }} - {{ page_obj.end_index }} 条，共 {{ page_obj.paginator.count }} 条记录
                        （第 {{ page_obj.number }} 页，共 {{ page_obj.paginator.num_pages }} 页）
                    </div>
                </nav>
                {% endif %}

                {% else %}
                <div class="text-center py-5">
                    <i class="bi bi-people display-1 text-muted"></i>
                    <h4 class="text-muted mt-3">暂无用户</h4>
                    <p class="text-muted">系统中还没有用户</p>
                </div>
                {% endif %}
            </div>
        </div>
    </div>
</div>
{% endblock %}
